package com.juzi.wopu.web;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.juzi.wopu.domain.CartCheck;
import com.juzi.wopu.domain.CartCheckResponse;
import com.juzi.wopu.domain.SysCode;
import com.juzi.wopu.domain.Type;
import com.juzi.wopu.query.CartCheckQuery;
import com.juzi.wopu.query.PageList;
import com.juzi.wopu.service.ICartCheckService;
import com.juzi.wopu.service.ISysCodeService;
import com.juzi.wopu.service.ISysUserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

import schemasMicrosoftComVml.STExt;

public class CartCheckAction extends CRUDAaction<CartCheck> {

	private static final long serialVersionUID = 1L;

	private ICartCheckService cartCheckService;
	
	private ISysUserService sysUserService;
	
	private ISysCodeService sysCodeService;

	private CartCheck cartCheck = new CartCheck();
	
	private CartCheckQuery baseQuery = new CartCheckQuery();
	
	private PageList pageList;
	
	private String totalNum;
	private String totalMoney;
	private String totalOrder;
	
	private List<CartCheckResponse> data=new ArrayList<>();
	
	public void setCartCheckService(ICartCheckService cartCheckService) {
		this.cartCheckService = cartCheckService;
	}
	
	public void setSysUserService(ISysUserService sysUserService) {
		this.sysUserService = sysUserService;
	}
	
	public ISysCodeService getSysCodeService() {
		return sysCodeService;
	}

	public void setSysCodeService(ISysCodeService sysCodeService) {
		this.sysCodeService = sysCodeService;
	}

	@Override
	protected void list() {
		
		//得到所有类型列表
		Map<Long, SysCode> syscodeMap=new HashMap<>();
		sysCodeService.getSysCodeMap(syscodeMap, null);
		List<Type> typeList=new ArrayList<>();
		Collection<SysCode> sysCodes=syscodeMap.values();
		for(SysCode sysCode:sysCodes){
			if(sysCode.getiType()==1L && sysCode.getIParentId()==99L){
				Type type=new Type();
				type.setTypeCode(sysCode.getiId());
				type.setTypeName(sysCode.getsName());
				type.setTotalTypeCode(sysCode.getiId()+"");
				type.setTotalTypeName(sysCode.getsName());
				typeList.add(type);
				for(SysCode sCode:sysCodes){
					if(sCode.getiParentId()!=null && 
						sCode.getiParentId().intValue()==sysCode.getiId().intValue()){
						Type type1=new Type();
						type1.setTypeCode(sysCode.getiId());
						type1.setTypeName(sysCode.getsName());
						type1.setSubTypeCode(sCode.getiId());
						type1.setSubTypeName(sCode.getSName());
						type1.setTotalTypeCode(sysCode.getiId()+" - "+sCode.getiId());
						type1.setTotalTypeName(sysCode.getsName()+" - "+sCode.getsName());
						typeList.add(type1);
					}
				}
			}
		}
		putContext("typeList", typeList);
		
		//得到所有订单状态
		List<SysCode> statusList=new ArrayList<>();
		for(SysCode sysCode:syscodeMap.values()){
			if(sysCode.getiType()==20L){
				statusList.add(sysCode);
			}
		}
		putContext("statusList", statusList);
		
		
		if(companyName!=null){
			ActionContext.getContext().getSession().put("companyName", companyName);
		}
		else {
			companyName=(String) ActionContext.getContext().getSession().get("companyName");
		}
		
		//查询所有的cartCheck
		List<CartCheckResponse> allDataList=cartCheckService.queryAllCartCheck();
		
		List<CartCheckResponse> list=new ArrayList<>();
		if(allDataList!=null && allDataList.size()>0){
			for(CartCheckResponse response:allDataList){
				if(!response.getCompanyName().equals(companyName)){
					continue;
				}
				if(baseQuery.getBeginDate()!=null && 
						response.getDtInsert().before(baseQuery.getBeginDate())){
					continue;
				}
				
				if(baseQuery.getEndDate()!=null && 
						response.getDtInsert().after(baseQuery.getEndDate())){
					continue;
				}
				if(baseQuery.getType()!=null && !baseQuery.getType().equals("-1")){
					String[] types=baseQuery.getType().split(" - ");
					if(response.getiType()!=Long.parseLong(types[0])){
						continue;
					}else if(types.length>1){
						if(response.getITypeSub()!=Long.parseLong(types[1])){
							continue;
						}
					}
				}
				
				if(baseQuery.getiStatus()!=null && baseQuery.getiStatus()!=-1L){
					if(response.getiStatus()==null || 
						response.getiStatus().intValue()!=baseQuery.getiStatus().intValue()){
						continue;
					}
				}
				list.add(response);
			}
		}
		
		totalOrder=list.size()+"";
		long count=0;
		double money=0;
		for(int i=0;i<list.size();i++){
			count=count+list.get(i).getiCount();
			money=money+list.get(i).getdPriceSum();
		}
		totalNum=count+"";
		totalMoney=money+"";
		
		
		
		if(pageList==null){
			pageList=new PageList();
			pageList.setCurrentPage(baseQuery.getCurrentPage());
			pageList.setPageSize(baseQuery.getPageSize());
		}
		pageList.setTotalcount(list.size());
		if(pageList.getTotalcount() % pageList.getPageSize()==0){
			pageList.setTotalPage(pageList.getTotalcount() / pageList.getPageSize());
		}else {
			pageList.setTotalPage(pageList.getTotalcount() / pageList.getPageSize()+1);
		}
				
		for(int i=pageList.getBegin();i<=pageList.getEnd();i++){
			if(i>list.size()){
				break;
			}
			data.add(list.get(i-1));
		}
	}
	
//	protected void list() {
//		Map<Long, SysUser> sysUserMap=new HashMap<>();
//		sysUserService.getSysUserMap(sysUserMap, null);
//		this.pageList = cartCheckService.findByQuery(baseQuery);
//		if(pageList!=null && pageList.getRows()!=null && pageList.getRows().size()>0){
//			totalOrder=pageList.getTotalcount()+"";
//			Map<String, String> map=cartCheckService.getTotalCount(baseQuery);
//			totalMoney=map.get("totalMoney");
//			totalNum=map.get("totalNum");
//		}else {
//			totalMoney="0.0";
//			totalOrder="0";
//			totalNum="0";
//		}
//		if(pageList!=null && pageList.getRows()!=null && pageList.getRows().size()>0){
//			for(int i=0;i<pageList.getRows().size();i++){
//				CartCheck cartCheck=(CartCheck) pageList.getRows().get(i);
//				CartCheckResponse response=new CartCheckResponse(cartCheck);
//				//response.setUserName(sysUserMap.get(cartCheck.getId()).getSUserNo());
//			
//				Long roleId = cartCheck.getiUserId();
//				 SysUser sysUser = sysUserMap.get(roleId);
//				if(roleId == null || sysUser == null){
//					response.setUserName("");
//				}else{
//					String getsName = sysUser.getsUserNo();
//					response.setUserName(getsName);
//				}
//				data.add(response);
//			}
//		}
//	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	public void validateSave() {
		if (StringUtils.isBlank(cartCheck.getsName())) {
			addFieldError("name", "名称不能为空");
		}
	}

	@Override
    @InputConfig(methodName="input")
	public String save() throws Exception {
		if(id==null){
			baseQuery.setCurrentPage(Integer.MAX_VALUE);
		}
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			if (id != null) {
				cartCheckService.delete(id);
				out.print("{\"success\":true,\"msg\":\"删除成功\"}");
			}else{
				out.print("{\"success\":false,\"msg\":\"没有删除成功\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"success\":false,\"msg\":\"异常信息:"+e.getMessage()+"\"}");
		}
		return null;
	}

	@Override
	public void prepareInput() throws Exception {
		if (id != null) {
			cartCheck = cartCheckService.get(id);
		}
	}

	@Override
	public void prepareSave() throws Exception {
		if (id == null) {
			cartCheck = new CartCheck();
		} else {
			cartCheck = cartCheckService.get(id);
		}
	}

	@Override
	public CartCheck getModel() {
		return cartCheck;
	}

	public PageList getPageList() {
		return pageList;
	}

	public CartCheckQuery getBaseQuery() {
		return baseQuery;
	}

	public void setBaseQuery(CartCheckQuery baseQuery) {
		this.baseQuery = baseQuery;
	}

	public List<CartCheckResponse> getData() {
		return data;
	}

	public void setData(List<CartCheckResponse> data) {
		this.data = data;
	}

	public String getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(String totalNum) {
		this.totalNum = totalNum;
	}

	public String getTotalMoney() {
		return totalMoney;
	}

	public void setTotalMoney(String totalMoney) {
		this.totalMoney = totalMoney;
	}

	public String getTotalOrder() {
		return totalOrder;
	}

	public void setTotalOrder(String totalOrder) {
		this.totalOrder = totalOrder;
	}
}
